<template>
   <el-select :value="defaultValue" @clear="clearSelect" @change="handleChange" :clearable="isAll" :disabled="disabled" :style="selectStyle">
    <el-option
      v-for="item in options"
      :key="item.bm"
      :label="item.mc"
      :value="item.bm">
    </el-option>
  </el-select>
</template>

<script>
import { query } from '@/api/common'

export default {
  // 非pcode类数据 调用下拉框展示
  name: 'defCombox',
  data() {
    return {
      defaultValue: '',
      options: []
    }
  },
  props: {
    disabled: {
      type: Boolean,
      default: function() {
        return false
      }
    },
    isAll: {
      type: Boolean,
      default: function() {
        return true
      }
    },
    value: {
      type: String
    },
    url: {
      type: String
    },
    queryparam: {
      type: Object,
      required: false,
      default: function() {
        return {}
      }
    },
    selectStyle: {
      type: Object,
      required: false,
      default: function() {
        return { width: '100%' }
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    handleChange(val) {
      this.$emit('input', val)
    },
    clearSelect() {
      this.defaultValue = ''
      this.$emit('input', '')
    },
    init() {
      // const url = 'archives/common/getMdcIdSelect'
      query(this.url, this.queryparam).then((result) => {
        const data = result.data.data
        if (data) {
          this.options = data
          if (!this.isAll && this.options.length > 0) {
            this.defaultValue = this.value || this.options[0].bm
            this.$emit('input', this.defaultValue)
          } if (this.value) {
            this.defaultValue = this.value
          }
        }
      })
    }
  },
  watch: {
    value() {
      this.defaultValue = this.value
    },
    queryparam: {
      handler: function(newvalue) {
        this.init()
      },
      deep: true
    }
  }
}
</script>